package com.share.sharelibrary.share;

import android.graphics.Bitmap;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.IntDef;

import com.alibaba.android.arouter.launcher.ARouter;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * 需要分享的数据
 *
 * @author QiQi
 * @date 2017/6/30
 */

public class ShareData implements Parcelable {
    /**
     * 分享链接
     */
    public final static int SHARE_WEB = 1;
    /**
     * 分享文本
     */

    public final static int SHARE_TEXT = 2;
    /**
     * 分享图片
     */

    public final static int SHARE_IMAGE = 3;
    /**
     * 分享音乐
     */
    public final static int SHARE_MUSIC = 4;
    /**
     * 分享视频
     */

    public final static int SHARE_VIDEO = 5;
    public static final Parcelable.Creator<ShareData> CREATOR = new Parcelable.Creator<ShareData>() {
        @Override
        public ShareData createFromParcel(Parcel source) {
            return new ShareData(source);
        }

        @Override
        public ShareData[] newArray(int size) {
            return new ShareData[size];
        }
    };
    private int shareType;

    private String text;
    private String url;
    private Bitmap bitmap;
    private String thumbUrl;

    private ShareData(int shareType, String text, String url, Bitmap bitmap, String thumbUrl) {
        this.shareType = shareType;
        this.text = text;
        this.url = url;
        this.bitmap = bitmap;
        this.thumbUrl = thumbUrl;
    }


    protected ShareData(Parcel in) {
        this.shareType = in.readInt();
        this.text = in.readString();
        this.url = in.readString();
        this.bitmap = in.readParcelable(Bitmap.class.getClassLoader());
    }

    @ShareType
    public int getShareType() {
        return shareType;
    }

    public String getText() {
        return text;
    }

    public String getUrl() {
        return url;
    }

    public Bitmap getBitmap() {
        return bitmap;
    }

    public String getThumbUrl() {
        return thumbUrl == null ? "" : thumbUrl;
    }

    public void startShare() {
        ARouter.getInstance()
                .build("/share/share")
                .withParcelable(ShareBroadView.ACTION_SHARE_DATA, this)
                .navigation();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.shareType);
        dest.writeString(this.text);
        dest.writeString(this.url);
        dest.writeParcelable(this.bitmap, flags);
    }

    @IntDef({
            SHARE_WEB, SHARE_TEXT, SHARE_IMAGE, SHARE_MUSIC, SHARE_VIDEO
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface ShareType {
    }

    public static class Builder {

        private int shareType;

        private String text;
        private String url;
        private Bitmap bitmap;
        private String thumbUrl;


        public Builder setShareType(@ShareType int shareType) {
            this.shareType = shareType;
            return this;
        }

        public Builder setText(String text) {
            this.text = text;
            return this;
        }


        public Builder setUrl(String url) {
            this.url = url;
            return this;
        }


        public Builder setBitmap(Bitmap bitmap) {
            this.bitmap = bitmap;
            return this;
        }

        public Builder setThumbUrl(String thumbUrl) {
            this.thumbUrl = thumbUrl;
            return this;
        }

        public ShareData builder() {
            return new ShareData(shareType, text, url, bitmap, thumbUrl);
        }

    }
}
